package dao;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public abstract class HibernateDAO {
	
	public static Session loadSession(){
		Session se =  util.HibernateUtil.getSessionFactory().openSession();
        return se;
	}
        
    protected static List getList(String sql){
        List lst = null;
        Session se=loadSession();        
        Transaction trans = null;
        try
        {
            trans = se.beginTransaction();
            Query q = se.createQuery(sql);
            lst =  q.list();
            trans.commit();
        }
        catch(Exception ex)
        {
            trans.rollback();
            System.err.print(ex);
        }
        finally
        {
            se.close();
        }

       return lst;
    }

    protected static List getList(String sql,int pageNumber, int pageSize)
    {
        List lst = null;
        Session se=loadSession();
        Transaction trans = null;
        try
        {
            trans = se.beginTransaction();
            Query q = se.createQuery(sql);
            q.setFirstResult(pageSize * pageNumber); //Set trang bắt đầu
            q.setMaxResults(pageSize);  //Set kích thướt 1 trang
            lst =  q.list();
            trans.commit();
        }
        catch(Exception ex)
        {
            trans.rollback();
            System.err.print(ex);
        }
        finally
        {
            se.close();
        }

       return lst;
    }
    
    protected static List[] getListSize(String sql,int pageNumber, int pageSize)
    {
    	List[] arrays=new List[2];
    	
        List lst = null;
        Session se=loadSession();
        Transaction trans = null;
        try
        {
            trans = se.beginTransaction();
            Query q = se.createQuery(sql);
            arrays[0]=q.list();
            q.setFirstResult(pageSize * pageNumber); //Set trang bắt đầu
            q.setMaxResults(pageSize);  //Set kích thướt 1 trang
            lst =  q.list();
            arrays[1]=lst;
            trans.commit();
        }
        catch(Exception ex)
        {
            trans.rollback();
            //ex.printStackTrace();
           System.err.print(ex);
        }
        finally
        {
            se.close();
        }

       return arrays;
    }

    /**Rút trích danh sách (có phân trang) and get ListMaps*/
    @SuppressWarnings({ "rawtypes", "unchecked" })
	protected static Map getListMap(String sql,int pageNumber, int pageSize)
    {
    	Map maps=new HashMap();
    	
    	Session se=loadSession();
        Transaction trans = null;
        try
        {
            trans = se.beginTransaction();
            Query q = se.createQuery(sql);
            maps.put("rows",q.list().size());
            q.setFirstResult(pageSize * pageNumber); //Set trang bắt đầu
            q.setMaxResults(pageSize);  //Set kích thướt 1 trang
            maps.put("list",q.list());
            trans.commit();
        }
        catch(Exception ex)
        {
            trans.rollback();
            //ex.printStackTrace();
           System.err.print(ex);
        }
        finally
        {
            se.close();
        }

       return maps;
    }

    /** Get Object in Id (String)
     *  Class<?> A : Object 
     *  Id: ObjectId
     */
    protected static Object getObject(Class<?> A, Serializable  Id)
    {
        Object ob = new Object();
        Session se=loadSession();
        Transaction trans = null;
        try
        {

           trans = se.beginTransaction();
           ob = (Object)se.get(A,Id);
           trans.commit();
          }
        catch(Exception ex)
        {
            trans.rollback();
            //ex.printStackTrace();
            System.err.print(ex);
        }
        finally
        {
            se.close();
        }

        return ob;
    }
    
    /**Thêm đối tượng mới*/
    protected static boolean insert(Object ob)
    {
        boolean bol = true;
        Session se=loadSession();
        Transaction trans = null;
        try
        {
            trans = se.beginTransaction();
            se.save(ob);
            trans.commit();
        }
        catch(Exception ex)
        {
            bol =  false;
            trans.rollback();
            System.err.print(ex);
            //ex.printStackTrace();
        }
        finally
        {
            se.close();
        }
        return bol;
    }

    /**Xóa đối tượng mới*/
    protected static boolean delete(Object ob)
    {
        boolean bol = true;
        Session se=loadSession();
        Transaction trans = null;
        try
        {
            trans = se.beginTransaction();
            se.delete(ob);
            trans.commit();
        }
        catch(Exception ex)
        {
            bol = false;
            trans.rollback();
            //ex.printStackTrace();
            System.err.print(ex);
        }
        finally
        {
            se.close();
        }
        return bol;
    }

    /**Cập nhật đối tượng*/
    protected static boolean update(Object ob)
    {
        boolean bol = true;
        Session se=loadSession();
        Transaction trans = null;
        try
        {
            trans = se.beginTransaction();
            se.update(ob);
            trans.commit();
        }
        catch(Exception ex)
        {
            bol = false;
            trans.rollback();
            System.err.print(ex);
            //ex.printStackTrace();
        }
        finally
        {
            se.close();
        }
        return bol;
    }
}
